package com.example.androiddemo.gallery;

import java.util.ArrayList;
import java.util.List;

import com.example.androiddemo.R;
import com.example.androiddemo.entities.MediaInfo;
import com.example.androiddemo.grid.GridViewAndRelateLayoutActivity;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.exception.DbException;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.TextView;

/**
 * 显示图集的界�?
 * @author kongqiangwang
 *
 */
public class PictureShowActivity extends Activity
{

	private static final String LOG_TAG = "PictureShowActivity";
	
	public static final String DATA_FLAG = "pictureInfoList";
	
	/** 屏幕宽度 */
	public static int screenWidth;
	
	/** 屏幕高度 */
	public static int screenHeight;
	
	/** 返回按钮 */
	private Button btBack;
	
	/** 图片说明 */
	private TextView tvInstruction;
	
	/** 图片个数 */
	private TextView tvNumber;
	
	/** 自定义的图片集浏览的画廊  */
	private ZoomImageGallery gallery;
	
	private int selectionPostion;
	
	DbUtils db = null;
	
	/** 图片信息列表 */
	private List<MediaInfo> medias = new ArrayList<MediaInfo>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_picture_show);
		selectionPostion = getIntent().getIntExtra("postion", 0);
		initView();
		initData();
	}
	
	private void initView()
	{
		btBack = (Button)findViewById(R.id.btBack);
		tvInstruction = (TextView)findViewById(R.id.tvInstruction);
		tvNumber = (TextView)findViewById(R.id.tvNumber);
		gallery = (ZoomImageGallery)findViewById(R.id.galleryPictureSet);
		
		btBack.setOnClickListener(clickListener);
		
		gallery.setVerticalFadingEdgeEnabled(false);// 取消竖直渐变边框
		gallery.setHorizontalFadingEdgeEnabled(false);// 取消水平渐变边框

		gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View view,
					int position, long arg3)
			{
				tvInstruction.setText("图片展示");
				tvNumber.setText(position  + 1 + "/" + medias.size());
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0)
			{
				
			}
			
		});
	}
	
	private void initData()
	{
		db = DbUtils.create(PictureShowActivity.this);
		
		try {
			medias = db.findAll(Selector.from(MediaInfo.class).where(MediaInfo.COLUMN_BCID, "=", 1));
			if(medias == null){
				medias = new ArrayList<MediaInfo>();
			}
		} catch (DbException e) {
			e.printStackTrace();
		}
		
		DisplayMetrics outMetrics = new DisplayMetrics();
		getWindow().getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
		screenWidth = outMetrics.widthPixels;
		screenHeight = outMetrics.heightPixels;
		
		gallery.setAdapter(new PictureShowAdapter(this, medias));
		gallery.setSelection(selectionPostion);

	}
	
	OnClickListener clickListener = new OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			switch(v.getId())
			{
				case R.id.btBack:
				{
					finish();
				}
				
				default:
				{
					break;
				}
			}
		}
		
	};
	
}
